package simplehdfs;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;

/**
 * 从HDFS读取文件
 * 打包运行jar包 [bruce@node01 Desktop]$ hadoop jar hadoop-1.0-SNAPSHOT.jar  org.example.hadoop.simple.ReadFileFromHDFS
 */
public class ReadFileFromHDFS {


    /**
     * @param args
     * args0 hdfs上文件hdfs://node01:8020/test.txt
     * args1 windows本地磁盘文件E:/test.txt
     */
    public static void main(String[] args){
        try{
            String srcFile = args[0];
            Configuration conf = new Configuration();

            FileSystem fs = FileSystem.get(URI.create(srcFile),conf);
            FSDataInputStream inputStream = fs.open(new Path(srcFile));

            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(args[1]));
            IOUtils.copyBytes(inputStream,outputStream,4096,true);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
